app.controller("createProjectCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "planId", function ($scope, $rootScope, $modalInstance, $http, planId) {
    $scope.info = {};
    $scope.info.planId = planId;
    $scope.info.radioStatus = "true";
    //项目信息
    $scope.info.planInfo = {};
    //选中的trunk name
    $scope.info.trunkName = "";
    //可选择的主干工程列表
    $scope.info.showTrunkList = [];
    //要保存的工程信息
    $scope.info.projectInfo = {};
    $scope.info.svn_form = "";
    $scope.info.msg = '';

    //选择已有工程时参数
    $scope.info.trunkInfo = {};
    //工程列表
    $scope.info.trunkList = [];
    $scope.info.projectList = [];

    //switch radio btn
    $scope.radioChange = function () {
        if ($scope.info.radioStatus == "true") {
            $scope.info.projectInfo = {};
            $scope.info.trunkName = "";
            $scope.info.svn_form = "";
            $scope.info.msg = "";
            $scope.hideIcon = true;
        } else {
            $scope.getBeetleTrunkList();
        }
    };

    //获取beetle管理的主干列表
    $scope.getBeetleTrunkList = function () {
        $scope.info.trunkList = [];
        var postParams = {
            tokenId: getTokenId(),
            onlyTrunk: true,
            isShowJar: true
        };
        $rootScope.showPanelLoading();
        var url = java_url + "/beetle/project/projectInfoList";
        $.ajax({
            type: 'post',
            url: url,
            data: postParams,
            dataType:'json',
            success: function (data) {
                if (data.code != '0') {
                    $rootScope.showMessage("error", "失败", "获取工程信息失败！desc=" + data.desc);
                    return false;
                }

                $scope.$apply(function($scope){
                    angular.forEach(data.data.list, function (trunk) {
                        var isExist = 0;
                        angular.forEach($scope.info.planInfo.projects, function (pro) {
                            if (trunk.projectName == pro.trunk_name) {
                                isExist = 1;
                                return false;
                            }
                        });
                        if (isExist == 0) {
                            $scope.info.trunkList.push(trunk);
                        }
                    });
                    $scope.info.trunkInfo = $scope.info.trunkList[0];
                });
                $scope.getBeetleProjectListByTrunk();
                $rootScope.hidePanelLoading();
            },
            error: function (data) {
                alert(data.desc)
            }
        })
    };

    //根据主干获取beetle管理的工程列表
    $scope.getBeetleProjectListByTrunk = function () {
        $scope.info.projectList = [];
        var postParams = {
            tokenId: getTokenId(),
            trunkName: $scope.info.trunkInfo.projectName
        };
        var url = java_url + "/beetle/project/projectListByTrunk";
        $.ajax({
            type: 'post',
            url: url,
            data: postParams,
            dataType:'json',
            success: function (data) {
                if (data.code != '0') {
                    $rootScope.showMessage("error", "失败", "获取工程信息失败！desc=" + data.desc);
                    return false;
                }
                $scope.$apply(function($scope){
                    $scope.info.projectList = data.data.list;
                    $scope.info.projectInfo = $scope.info.projectList[0];
                });
            },
            error: function (data) {
                alert(data.desc)
            }
        })
    };

    //获取项目信息
    $scope.getPlanInfo = function () {
        var url = beetle_plan_url + "/project_plan/getPlanList?hasProList=true&isShowOnline=true&token_id=" + getTokenId() + "&plan_id=" + $scope.info.planId;
        $http.get(url).success(function (data) {
            $scope.info.planInfo = data.data[0];
            $scope.getTrunkProjectList();
        });
    };

    $scope.getPlanInfo();

    $scope.getTrunkProjectList = function () {
        var url = beetle_plan_url + "/project/getTrunkProjectList?token_id=" + getTokenId();
        $rootScope.showPanelLoading();
        $http.get(url).success(function (data) {
            var list = data.data.trunkProject;
            angular.forEach(data.data.jarProject, function (jar) {
                list.push(jar);
            });
            angular.forEach(list, function (trunk) {
                var isExist = 0;
                angular.forEach($scope.info.planInfo.projects, function (pro) {
                    if (trunk.project_name == pro.trunk_name) {
                        isExist = 1;
                        return false;
                    }
                });
                if (isExist == 0) {
                    $scope.info.showTrunkList.push(trunk);
                }
            });
            $rootScope.hidePanelLoading();
        });
    };

    $scope.selectChange = function () {
        var list = [];
        $scope.info.svn_form = "trunk";
        if ($scope.info.trunkName == "") {
            return false;
        }
        list.push($scope.info.trunkName);

        var url = beetle_plan_url + "/project_plan/getPlanProjectDefaultName?token_id=" + getTokenId() + "&plan_code=" + $scope.info.planInfo.plan_code + "&trunk_list=" + JSON.stringify(list);
        $http.get(url).success(function (data) {
            $scope.info.projectInfo = data.data[0];
        });
    };
    $scope.updateBranch_num = function () {
        var branch_num = $scope.info.projectInfo.branch_num;
        var t = /^(\d{1,4}){1}(\.\d{1,4}){2}$/;
        if (branch_num.match(t)) {
            $scope.info.branch_numLock = false;
            $scope.info.msg = '';
        } else {
            $scope.info.branch_numLock = true;
            $scope.info.msg = '请输入正确的版本号！格式：0.0.0';
        }
    };

    //save btn
    $scope.saveClick = function () {
        if ($scope.info.radioStatus == "true") {
            if ($scope.info.trunkName == "") {
                $rootScope.showMessage("error", "失败", "工程不能为空，请选择工程！");
                return false;
            }
            if ($scope.info.svn_form == "") {
                $rootScope.showMessage("error", "失败", "工程来源不能为空，请选择工程来源！");
                return false;
            }
            $scope.info.project_name = $scope.info.projectInfo.trunk_name + '-' + $scope.info.projectInfo.branch_num + '.' + $scope.info.planInfo.plan_code;
            var url = beetle_plan_url + "/project_plan/addPlanProject?project_name=" + $scope.info.project_name + "&plan_id=" + $scope.info.planInfo.plan_id + "&branchNum=" + $scope.info.projectInfo.branch_num + "&trunk_name=" + $scope.info.trunkName + "&token_id=" + getTokenId() + "&fromProject=" + $scope.info.svn_form;
            $http.get(url).success(function (data) {
                if (data.success) {
                    $modalInstance.close();
                } else {
                    $rootScope.showMessage("error", "失败", data.msg);
                }
            });
        } else {
            var url = beetle_plan_url + "/project_plan/addPlanProjectExist?project_name=" + $scope.info.projectInfo.projectName + "&plan_id=" + $scope.info.planInfo.plan_id + "&trunk_name=" + $scope.info.trunkInfo.projectName + "&token_id=" + getTokenId();
            $http.get(url).success(function (data) {
                if (data.success) {
                    $modalInstance.close();
                } else {
                    $rootScope.showMessage("error", "失败", data.msg);
                }
            });
        }
    };

    //close btn
    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);